package com.aysafety.iotdataplatformweb.services.kgt.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.aysafety.core.DBDriver.hbase.HBaseDriver;
import com.aysafety.core.common.DateUtils;
import com.aysafety.iotdataplatformweb.services.kgt.KGTService;

@Service
public class KGTServiceImpl implements KGTService {
    private final Map<String, Object> currentBreakerStructure = new HashMap<String, Object>() {
        {
            put("entId", "String");
            put("unit", "String");
            put("monitoringValue", "Double");
            put("poklad", "Double");
            put("humidityD", "String");
            put("datetime", "Date");
            put("workingFace", "String");
            put("deviceId", "String");
            put("smallestRange", "Double");
            put("maximumRange", "Double");
            put("stationName", "String");
            put("status", "Boolean");
            put("powerCutArea", "String");
            put("powerReconnectionLimit", "Double");
            put("powerCutLimit", "Double");
        }
    };

    public long countByKGT(String mineId, String deviceId, String startTime, String endTime, int timeSpan,
        String timeUnit) throws Throwable {
        List<Map<String, Object>> conditions = new ArrayList<Map<String, Object>>();
        Map<String, Object> parram = new HashMap<String, Object>();
        String startRow = "";
        String endRow = "";
        parram.put("columnFamily", "CurrentBreaker");
        parram.put("columnId", "entId");
        parram.put("valueType", "String");
        parram.put("value", mineId);
        parram.put("symbol", "EQUAL");
        conditions.add(parram);
        parram = new HashMap<String, Object>();
        parram.put("columnFamily", "CurrentBreaker");
        parram.put("columnId", "deviceId");
        parram.put("valueType", "String");
        parram.put("value", deviceId);
        parram.put("symbol", "EQUAL");
        conditions.add(parram);
        parram = new HashMap<String, Object>();
        parram.put("columnFamily", "CurrentBreaker");
        parram.put("columnId", "datetime");
        parram.put("valueType", "Long");
        parram.put("value", DateUtils.fomatDateTime(startTime).getTime());
        parram.put("symbol", "GREATER_OR_EQUAL");
        conditions.add(parram);
        parram = new HashMap<String, Object>();
        parram.put("columnFamily", "CurrentBreaker");
        parram.put("columnId", "datetime");
        parram.put("valueType", "Long");
        parram.put("value", DateUtils.fomatDateTime(endTime).getTime());
        parram.put("symbol", "LESS_OR_EQUAL");
        conditions.add(parram);
        if (StringUtils.isNotBlank(startTime)) {
            endRow = String.valueOf(DateUtils.fomatDateTime(startTime).getTime()) + ".";
        }
        if (StringUtils.isNotBlank(endTime)) {
            startRow = String.valueOf(DateUtils.fomatDateTime(endTime).getTime()) + ".";
        }
        return HBaseDriver.countData("CurrentBreakerRealTime", conditions, startRow, endRow);
    }

    public List<Map<String, Object>> queryRealTimeByKGT(String mineId, String deviceId, String startTime,
        String endTime, int timeSpan, String timeUnit, int page, int size, String startRow) throws Throwable {
        List<Map<String, Object>> conditions = new ArrayList<Map<String, Object>>();
        Map<String, Object> parram = new HashMap<String, Object>();
        String endRow = "";
        parram.put("columnFamily", "CurrentBreaker");
        parram.put("columnId", "entId");
        parram.put("valueType", "String");
        parram.put("value", mineId);
        parram.put("symbol", "EQUAL");
        conditions.add(parram);
        parram = new HashMap<String, Object>();
        parram.put("columnFamily", "CurrentBreaker");
        parram.put("columnId", "deviceId");
        parram.put("valueType", "String");
        parram.put("value", deviceId);
        parram.put("symbol", "EQUAL");
        conditions.add(parram);
        parram = new HashMap<String, Object>();
        parram.put("columnFamily", "CurrentBreaker");
        parram.put("columnId", "datetime");
        parram.put("valueType", "Long");
        parram.put("value", DateUtils.fomatDateTime(startTime).getTime());
        parram.put("symbol", "GREATER_OR_EQUAL");
        conditions.add(parram);
        parram = new HashMap<String, Object>();
        parram.put("columnFamily", "CurrentBreaker");
        parram.put("columnId", "datetime");
        parram.put("valueType", "Long");
        parram.put("value", DateUtils.fomatDateTime(endTime).getTime());
        parram.put("symbol", "LESS_OR_EQUAL");
        conditions.add(parram);
        if (StringUtils.isNotBlank(startTime) && StringUtils.isBlank(startRow)) {
            endRow = String.valueOf(DateUtils.fomatDateTime(startTime).getTime()) + ".";
        }
        if (StringUtils.isNotBlank(endTime)) {
            startRow = String.valueOf(DateUtils.fomatDateTime(endTime).getTime()) + ".";
        }
        return HBaseDriver.queryData("CurrentBreakerRealTime", conditions, page, size, this.currentBreakerStructure,
            startRow, endRow);
    }
}
